<?php

namespace App\Models;


use App\Http\Traits\GlobalConstant;

class Benefit extends Orm
{
    //
    protected $table = 'benefit';
    protected $dateFormat = 'U';
    public $timestamps = true;
    protected $casts = [
//        'partner' => 'json',
//        'consult' => 'json',
        'pics'    => 'json'
    ];

    public function campusInfo()
    {
        return $this->belongsTo(Campus::class, 'campus_id');
    }

    public function getTagsAttribute()
    {
        return $this->attributes['tags'] ?? '';
    }

    public function setApprovalAtAttribute($value)
    {
        $this->attributes['approval_at'] = strtotime($value);
    }

    public function getApprovalAtAttribute()
    {
        return date('Y年m月', $this->attributes['approval_at']);
    }

    public function getGenreAttribute()
    {
        return $this->attributes['genre'] == 1 ? '线下' : '线上';
    }

    public function setPicsAttribute($value)
    {
        $this->dealSetImageAttribute('pics', $value);
    }

    public function getPartnerAttribute()
    {
        if (empty($this->attributes['partner'])) return '';
        return json_decode($this->attributes['partner'], true);
    }

    public function setPartnerAttribute($value)
    {
        $attribute = GlobalConstant::resetArray($value);
        if (empty($attribute)) {
            $this->attributes['partner'] = '';
        } else {
            $this->attributes['partner'] = json_encode($attribute);
        }
    }

    public function getConsultAttribute()
    {
        if (empty($this->attributes['consult'])) return '';
        return json_decode($this->attributes['consult'], true);
    }

    public function setConsultAttribute($value)
    {
        $attribute = GlobalConstant::resetArray($value);
        if (empty($attribute)) {
            $this->attributes['consult'] = '';
        } else {
            $this->attributes['consult'] = json_encode($attribute);
        }
    }
}
